<?php
/*
  $Id: mp3manager.php,v 2.00 09/11/2004 16:00:10 ManMAchine Exp $

  Mp3 player module for osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com
  
  osPlayer - ManMAchine - 09/11/2004 manmachine@counteractdesign.com
  Copyright (c) 2004 ManMAchine

  Released under the GNU General Public License
*/
//********************************************
//********************************************
//      osPlayer V2 (c) Manmachine
//********************************************
//********************************************
/*
2007-06-24 no static info
*/
  require('includes/application_top.php');
	
	$product_mp3_id = $product_id ;   

    $product_mp3_query = tep_db_query("select pd.products_name, p.products_model  from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where  p.products_id = '" . $product_mp3_id . "' and pd.products_id = '" . $product_mp3_id . "' and pd.language_id = '" . (int)$languages_id . "'");
    $product_mp3 = tep_db_fetch_array($product_mp3_query);

    $product_folder_query = tep_db_query("select products_folder from " . TABLE_PRODUCTS_MP3 . " where products_id = '" . $product_mp3_id . "'");
    $product_folder = tep_db_fetch_array($product_folder_query);


    if (tep_not_null($product_folder['products_folder'])) {
	$current_path = DIR_FS_CATALOG_MP3 . $product_folder['products_folder'] .'/';
	} else {
    $current_path = DIR_FS_CATALOG_MP3 ;
    }
    $path = $current_path ;

	if((!file_exists(DIR_FS_CATALOG_MP3)) || (!is_dir(DIR_FS_CATALOG_MP3))) {
	   $ThisFileName = basename(__FILE__);
       	   $messageStack->add(PATH_NOT_FOUND);
	}

	if(($product_mp3['products_model'] == (""))) {
	   $ThisFileName = basename(__FILE__);
       	   $messageStack->add(HEADING_NO_MODEL_NUMBER . $product_mp3['products_name'] . HEADING_NO_MODEL_NUMBER_CORRECT);
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<script language="javascript" src="includes/general.js"></script>
<?php require(DIR_FS_TEMPLATE . 'scripts/javascript.php'); ?>
<link rel="stylesheet" href="<?php echo DIR_WS_TEMPLATE ?>css/style.css" type="text/css">
</head>
<body id="page" class="font-large width-wide" marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<?php require(DIR_FS_TEMPLATE . 'template.php'); ?>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" align="center" width="95%" cellspacing="0" cellpadding="0">
  <tr>
<?php
/*  
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
    </table></td>
*/
?>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
	<tr>
            <td class="didyouKnowBox"><img src="images/success.gif">&nbsp;<?php echo HEADING_CURRENT_PATH . $path ;?></td>
          </tr>
	<tr><td>&nbsp;</td></tr>
	  <tr>
	 <td class="didyouKnowBox"><?php echo 'Maximum Post Size = ' . ini_get('post_max_size') ;?><br>
	<?php echo 'Maximum Upload File Size = ' . ini_get('upload_max_filesize') ;?>	
	</td>
	   </tr>
	<tr><td>&nbsp;</td></tr>
	<tr>
             <td class=""><?php echo $filemanager ;?></td>
              </tr>      
	<tr>
            <td class="Heading1"><br><?php echo HEADING_TITLE; ?></td>
            
          </tr>
	<tr><td><br>
	<?php echo MP3_MANAGER_PRODUCT . $product_mp3['products_name']; ?><br>
	<?php echo MP3_MANAGER_MODEL . $product_mp3['products_model']; ?>
	</td></tr>
        </table></td>
      </tr>


      <tr>
        <td><table border="0" cellspacing="0" cellpadding="1">
          <tr>
		   
            <td width="100%" align="center" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2" align="center">
              <tr class="dataTableHeadingRow">			    
				  <td width="100%" align="center">
				  
<?

////delete directory and his content 
if(isset($deletedir)) { // if the delete directory button was pressed
	
            $totalfiles = tep_delete_mp3_folder($deletepath);
            tep_db_query("delete from " . TABLE_PRODUCTS_MP3 . " where products_id ='" . $HTTP_POST_VARS['product_id'] . "'");		
			$rootmsg = '<table align="center" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td class="didyouKnowBox" align="center"><img src="images/success.gif">&nbsp;' . FOLDER_DELETE_SUCCES . $product_mp3['products_model'] . '<br><img src="images/success.gif">&nbsp;' . TOTAL_DELETED_FILES . $totalfiles . '</td></tr></table><br>';
			$rootmsg .= '<table with="100%" cellpadding="0" cellspacing="0" border="0"><tr><td align="center"><div class="buttons"><a href="' . tep_href_link('mp3manager.php', 'product_id=' . $HTTP_POST_VARS['product_id'] ) . '" class="">' . tep_image_button('button_continue.gif', 'Continue') .'</a></div></td></tr></table>';
			$path ='';

	
	}

if((tep_not_null($product_folder['products_folder'])) && (file_exists(DIR_FS_CATALOG_MP3 . $product_folder['products_folder'])) && (is_dir(DIR_FS_CATALOG_MP3 . $product_folder['products_folder'])) && (!isset($deletedir))) {

$HiddenFiles = array(".htaccess","fileicon.gif","foldericon.gif","arrowicon.gif");
$MakeDirOn = 1;

	$ThisFileName = basename(__FILE__);



	if (isset($upload)) {// if the upload button was pressed
	
		if($HTTP_POST_FILES['uploadedfile']['name']) {// if a file was actually uploaded 
	
			$HTTP_POST_FILES['uploadedfile']['name'] = str_replace("%","",$HTTP_POST_FILES['uploadedfile']['name']);  // remove any % signs from the file name

			// put the file in the directory
			move_uploaded_file($HTTP_POST_FILES['uploadedfile']['tmp_name'], $path.$HTTP_POST_FILES['uploadedfile']['name']);
			@chmod($path.$HTTP_POST_FILES['uploadedfile']['name'], 0755);
				
		} else {
			$msg =  "" . UPLOAD_ERROR . "<br>";
		}
	}
	
	if(isset($delete)) {// if the delete button was pressed
	
		// delete the file or directory
		if(is_dir($path.$delete)) {
			$result = @rmdir($path.$delete);
			if($result == 0) {
				$msg = "". DELETE_ERROR . "<br>";
			}
		} else {
			unlink($path.$delete);
		}
	}
	
	// build the html that makes up the file manager
	// the $filemanager variable holds the first part of the html
	// including the form tags and the top 2 heading rows of the table which
	// dont display files
	$filemanager = <<<content
	<left>
	<table border='0' width='100%' cellspacing='0' cellpadding='0'>
	<tr>
	$msg
	<td align="left" class="dataTableRow">
	<br><div class="didyouKnowBox">Upload a new file.</div>
	<form name="form1" method="post" action="$PHP_SELF" enctype="multipart/form-data">	
  	<br><input type="file" name="uploadedfile" size="50">
  	<input type="submit" class="positive" name="upload" value="Upload">
    	<input type="hidden" name="product_id" value="$product_mp3_id">		
	</form><BR><BR>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" align="left">
	<tr class="infoBoxHeading"> 
	<td height="20" align="left"></td>
	<td height="20" align="left"><b>&nbsp;FILENAME&nbsp;</b></td>
	<td height="20" align="left"><b>&nbsp;SIZE (bytes)&nbsp;</b></td>
	<td height="20" align="left"><b>&nbsp;ACTION&nbsp;</b></td>
	<td height="20" align="left"></td>
	</tr>
	<tr> 
	<td height="2" align="left"></td>
	<td height="2" align="left"></td>
	<td height="2" align="left"></td>
	<td height="2" align="left"></td>
	<td height="2" align="left"></td>
	</tr>
content;


	// build the table rows which contain the file information
	$newpath = substr($path, 0, -1);   // remove the forward or backwards slash from the path
	$dir = @opendir($newpath); // open the directory
	while($file = readdir($dir)) {// loop once for each name in the directory
	
		// if the name is not a directory and the name is not the name of this program file
		if($file != "." && $file != ".." && $file != "$ThisFileName") {
			$match = 0;
			foreach($HiddenFiles as $name) {// for each value in the hidden files array
			
				if($file == $name) {// check the name is not the same as the hidden file name
					
					$match = 1;	 // set a flag if this name is supposed to be hidden
				}
			}
			
			if(!$match) { // if there were no matches the file should not be hidden
				
					$filedata = stat($path.$file); // get some info about the file
					
					// create some html for a link to delete files 
					$deletelink = "<div class=buttons><a href=\"$PHP_SELF?delete=$file&product_id=$product_mp3_id\">" .tep_image_button('button_delete.gif','Delete File')."</a></div>";
					
					// if it is a directory change the file name to a directory link
					if(is_dir($path.$file)) {
						$filename = "<div class=buttons><a href=\"$PHP_SELF?u=$u&pathext=$pathext$file&product_id=$product_mp3_id/\">$file</a></div>";
						$fileicon = '&nbsp;' . tep_image(DIR_WS_ICONS . 'fileicon.gif') . '&nbsp;';
					
					}else{
						$filename = $file;
						$fileicon = '&nbsp;' . tep_image(DIR_WS_ICONS . 'fileicon.gif') . '&nbsp;';
					}
					
					// append 2 table rows to the $content variable, the first row has the file
					// informtation, the 2nd row makes a black line 1 pixel high
					$content .= <<<content
					<tr valign="middle">
					<td align="left">$fileicon</td>
					<td align="left">&nbsp;$filename&nbsp;</td>
					<td align="left">&nbsp;$filedata[7]&nbsp;</td>
					<td align="left">&nbsp;$deletelink&nbsp;</td>
					<td align="left">&nbsp;</td>					
					</tr>
					<tr> 
					<td height="1" align="left" ></td>
					<td height="1" align="left"></td>
					<td height="1" align="left"></td>
					<td height="1" align="left"></td>
					<td height="1" align="left">&nbsp;</td>	
					</tr>	
content;
			}
		}
	}
closedir($dir); // now that all the rows have been built close the directory
$content .= "</td></tr></table></table></center>"; // add some closing tags to the $content variable
$filemanager  .= $content; // append the html to the $filemanager variable
?>
<table border="0" width="100%" cellspacing="0" cellpadding="2">
 <tr class="">
  <td align="left" class="" ><!-- <?php echo 'Current path :' . $path .'&nbsp;&nbsp; //--><form name="delete" method="post" action="' . $PHP_SELF . '" enctype="multipart/form-data"><input type="hidden" name="product_id" value="' . $product_mp3_id . '"><input type="hidden" name="deletepath" value="' . $path .'" ><input type="hidden" name="deletedir" value="deletedir"><BR><BR>' .tep_image_submit('button_delete.gif', 'Delete MP3 Directory') .'</form>';?>
<div class="buttons"><?php  echo '<a href="' . tep_href_link(FILENAME_CATEGORIES, '', 'NONSSL') . '" class="menuBoxContentLink">Back to Categories</a><div>';?>
</td>
   </tr>
    
<!--	
	<tr class="">
	 <td align="left" class=""><br><?php echo 'PHP.ini config >> post_max_size = ' . ini_get('post_max_size') ;?></td>
	   </tr>
	    <tr class="">
		 <td align="left" class=""><?php echo 'PHP.ini config >> upload_max_filesize = ' . ini_get('upload_max_filesize') ;?></td>
		  </tr>
//-->
		    <tr>
             <td align="center"><?php echo $filemanager ;?></td>
              </tr>

		   </table>
<?

} else {

	
   	
	if((!file_exists(DIR_FS_CATALOG_MP3)) || (!is_dir(DIR_FS_CATALOG_MP3))) {
	   $ThisFileName = basename(__FILE__);
       $path = PATH_NOT_FOUND ;
	   $rootmsg = ''.NO_MP3_FOLDER.'<br>';
	   $rootmsg .= '<a href="' . tep_href_link('mp3manager.php', 'product_id=' . $product_mp3_id ) . '" class="">' . tep_image_button('button_continue.gif', 'Enter') .'</a>';
	   
	}
	
	
	if((file_exists(DIR_FS_CATALOG_MP3)) && (!isset($mkrootdir)) && (!isset($deletedir)))	{
       $ThisFileName = basename(__FILE__);
	   $path = DIR_FS_CATALOG_MP3 ;
       	   $rootmsg = '<BR>' . NO_FOLDER . '<BR>';
	   $rootmsg .= '<form name="form2" method="post" action="' . $PHP_SELF . '" enctype="multipart/form-data"><input type="hidden" name="product_id" value="' . $product_mp3_id . '"><input type="text" name="dirname" value="' . $product_mp3['products_model'] .'" size="25" maxlength="25" >';
	   $rootmsg .= '<table align="center" with="100%" cellpadding="0" cellspacing="0" border="0"><tr><td align="center"><BR><input type="hidden" name="mkrootdir" value="Make Directory" alt="Create" title="Create"><BR>' . tep_image_submit('button_create_folder.gif','Create Directory') . '</td></tr></table>';
	   $rootmsg .= '</form>';
	} elseif ((file_exists(DIR_FS_CATALOG_MP3)) && (isset($mkrootdir)) && (!isset($deletedir)))  {
		$path = DIR_FS_CATALOG_MP3 ;
		$result = @mkdir($path.$dirname, 0777);
		
		if($result == 1) {
			@chmod($path.$dirname, 0777);
			tep_db_query("insert into " . TABLE_PRODUCTS_MP3 . " (products_id, products_folder) values ('" . (int)$HTTP_POST_VARS['product_id'] . "', '" . $HTTP_POST_VARS['dirname'] . "')");
			$path = DIR_FS_CATALOG_MP3 . stripslashes($HTTP_POST_VARS['dirname']) .'/';      
			$rootmsg = '<table align="center" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td class="didyouKnowBox" align="center"><img src="images/success.gif">&nbsp;'. FOLDER_SUCCES . $product_mp3['products_model'] .'</td></tr></table><br>';			
			$rootmsg .= '<table with="100%" align="center" cellpadding="0" cellspacing="0" border="0"><tr><td align="center"><div class="buttons"><a href="' . tep_href_link('mp3manager.php', 'product_id=' . $HTTP_POST_VARS['product_id'] ) . '" class="">' .	tep_image_button('button_continue.gif', 'Continue') .'</a></div></td></tr></table>';
		
		} else {

			$rootmsg = '<table align="center" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td class="tooltipBox" align="center"><img src="images/error.gif">&nbsp;' . ROOT_FOLDER_ERROR .'</td></tr></table><br>';
			$rootmsg .= '<table with="100%" align="center" cellpadding="0" cellspacing="0" border="0"><tr><td align="center"><div class="buttons"><a href="' . tep_href_link('mp3manager.php', 'product_id=' . $HTTP_POST_VARS['product_id'] ) . '" class="">' . tep_image_button('button_continue.gif', 'Continue') .'</a></div></td></tr></table>';
	}
	}	

?>
<table border="0" width="100%" cellspacing="0" cellpadding="2" align="left">
 <tr class="">
  <td align="center" class=""><!-- <?php echo 'current path:  ' . $path ;?> //--> </td>
   </tr>
     <tr class="">
	  <td align="center" class=""><?php echo $rootmsg ;?></td>
	   </tr>
	    </table>

<?

} 

?>
            </td>
              </tr>
            </table></td>
			<td align="left" valign="top"><table border="0"  cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow" align="left">
			    <td align="left" class="dataTableContent">
			    <object type="application/x-shockwave-flash" data="<?php echo DIR_WS_CATALOG; ?>buyx_player.swf?mp3id=<?php echo '' . $product_mp3_id .''?>&autoplay=true&buyURL=http://www.3am.co.za/catalog/product_info.php?products_id=<?php echo $HTTP_GET_VARS['products_id'];?>&coverURL=<?php echo "{$nano_result_rows['products_image']}";?>" width="150" height="388">
	                    <param name="quality" value="high" />
	                    <param name="bgcolor" value="#ffffff" />
	                    <param name="movie" value="<?php echo DIR_WS_CATALOG; ?>buyx_player.swf?mp3id=<?php echo '' . $product_mp3_id .''?>"/>
	                    <embed src="<?php echo DIR_WS_CATALOG; ?>buyx_player.swf?mp3id=<?php echo '' . $product_mp3_id .''?>" quality="high" bgcolor="#ffffff" width="150" height="388" name="movie" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
                            </object>
			    </td>
			  </tr>
			  </table>
			  </td>
          </tr>
      
        </table></td>
      </tr>

<tr>
 <td>&nbsp;</td>
  </tr>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
